package com.ssii.www.myproject.web.controller;

import com.ssii.www.myproject.common.util.AjaxResult;
import com.ssii.www.myproject.common.util.AjaxResultB;
import com.ssii.www.myproject.common.util.LogUtils;
import com.ssii.www.myproject.domain.System;
import com.ssii.www.myproject.domain.Task;
import com.ssii.www.myproject.query.SystemQuery;
import com.ssii.www.myproject.service.SystemService;
import com.ssii.www.myproject.service.TaskService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/autho")
@Transactional
public class AuthoController extends BaseController {
  private static final Logger logger = LogUtils.getLogger();
  @Autowired
  private SystemService systemService;

  @Autowired
  private TaskService taskService;

  @RequiresPermissions("autho:index")
  @RequestMapping("/index")
  public String index() {
    return "autho/index";
  }

  @RequestMapping("/page")
  @ResponseBody
  public Map<String, Object> page(SystemQuery query) {
    Map<String, Object> map = new HashMap<>();

    if (StringUtils.isNotBlank(query.getQ())) {
      query.setName(query.getQ());
    }
    Page<System> pageByQuery = systemService.findPageByQuery(query);
    map.put("rows", pageByQuery.getContent());
    map.put("page", pageByQuery.getTotalPages());
    map.put("total", pageByQuery.getTotalElements());
    return map;
  }

  /**
   * @param task
   * @return
   */
  @RequestMapping("/startProcessInstance")
  @ResponseBody
  public AjaxResult apply(Task task) throws Exception {
    List<Task> tasks = taskService.findTasksByApplyCode(task.getApplycode());
    //同一个申请码只存在一条未处理的记录
    for (Task task1 : tasks) {
      if(task1.getState()==1){
        return new AjaxResult("这个申请码已经在申请中。。。  ");
      }
    }
    //没有填写默认为当前时间
    if (task.getApplydate() == null)
      task.setApplydate(new Date());
    //申请码截止日期的限制
    if (task.getApplydate().compareTo(task.getDeadline()) > 0) {
      return new AjaxResult("截止日期不能小于申请日期");
    }
    taskService.save(task);
    //都只能比较同种格式的日期 可以前台匹配正则表达式 或者有对应的日期输入组件 或者后台验证
//      if(task.getApplydate().after(task.getDeadline())){
//        return new AjaxResult("截止日期不能小于申请日期");
//      }
//      long time = task.getApplydate().getTime();
//      long time1 = task.getDeadline().getTime();
//      if(time>time1){
//         return new AjaxResult("截止日期不能小于申请日期");
//      }
    return new AjaxResult();
  }

  @ResponseBody
  @RequestMapping("/queryApply")
  public AjaxResultB queryApply(String queryByApplycode) {
    List<Task> tasks = taskService.findTasksByApplyCode(queryByApplycode);
    if(tasks.size()==0){
      return new AjaxResultB("没有这个申请码");
    }
    if(tasks.size()>1){
      for (Task task : tasks) {
        //拿到状态为2的那条数据
        if(task.getState()==2){
          return new AjaxResultB(true, "授权码为" + task.getLicense(), task);
        }
      }
    }
    AjaxResultB ajaxResultB = null;
    for (Task task : tasks) {
      //其他的状态码都是已作废
      if (task != null && task.getState() == 3) {
        ajaxResultB =  new AjaxResultB("申请未通过,原因：" + task.getDenyrea());
      } else if (task != null && task.getState() == 2) {
        //把task也返回到页面
        ajaxResultB =  new AjaxResultB(true, "授权码为" + task.getLicense(), task);
      } else if (task != null && task.getState() == 1) {
        ajaxResultB = new AjaxResultB("你的申请未受理");
      }
    }

    return ajaxResultB;
  }

}

